package com.flyaudio.xiaopengautopilot.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class CalendarHandler {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    public final static int MAX_DATES = 366;

    Date startDate = null;
    private final String TAG = "CalendarHandler";

    public CalendarHandler()
    {

    }
    public void init(String data)
    {
        try {
            startDate = sdf.parse(data);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public Calendar startDateToNMonth(int nMonth)
    {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(startDate);
        rightNow.add(Calendar.MONTH, nMonth);
        return rightNow;
    }
    public Calendar startDateToNDay(int nDay)
    {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(startDate);
        rightNow.add(Calendar.DAY_OF_YEAR, nDay);
        return rightNow;
    }
    public String toString(Calendar calendar) {
        return sdf.format(calendar.getTime());
    }
    public long dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = sdf;
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天

            // 输出结果
            Log.d("CalendarHandler","时间相差：" + day + "天" );
            if (day>=1) {
                return day;
            }else {
                return 0;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }
}
